<?php
namespace doumiaoduo\telecaptcha;

use common\models\datas\CardAttr;
use common\models\datas\CardSend;
use common\models\datas\Customer;
use common\models\datas\IsuserInfo;
use common\models\datas\Apply;
use frontend\models\datas\Goods;
use frontend\models\datas\Mechanic;
use frontend\models\datas\Orders;
use frontend\models\datas\OrdersItems;
use yii\base\Action;
use yii\base\ActionEvent;
use yii\helpers\ArrayHelper;

class SendapplyfailAction extends Action
{

    public $customer_name = '';

    public $customer_tele = '';

    public $saleman_name = '无';

    const EVENT_SEND_SMS_MESSAGE = 'sendsmsmessage';

    public $behaviorsData = [];

    public $good_name = '';

    /*
     * 点击获取验证码所进行的操作
     * 1. 获取手机号
     * 2. 生成随机验证码，以及短信内容
     * 3. 发送短信
     * 4. 成功之后保存短信码和事件至session中；失败返回错误信息
     */
    public function run($apply_id= null)
    {
            $apply = Apply::findOne($apply_id);
            if (empty($apply->telephone)) {
                $data['result'] = 'failed';
            } else {
                $telenumber = $apply->telephone;
                /*start: 每天每个手机号获取限制次数的验证码*/
                $data['result'] = 'success';
                $res['telephone'] = $telenumber;
                $created_at= \Yii::$app->formatter->asDate($apply->created_at);
                $status = '申请未通过。';

                $res['created_at'] = $created_at;
                $res['status'] = $status;
                $this->sendSmsmsg($res);//触发事件

            }
            return json_encode($data);

    }

    //调用此方法触发发送短信的事件
    public function sendSmsmsg(array $res){
        $this->trigger(self::EVENT_SEND_SMS_MESSAGE, new ActionEvent(["data"=>$res]));
    }

    //重写行为
    public function behaviors()
    {
        return ArrayHelper::merge(parent::behaviors(), $this->behaviorsData);
    }


}